// src/pages/getcash/index.js
import getIn from '../../../yali-sdk/utils/getIn';
import { SUCCESS } from '../../../yali-sdk/utils/success';
import { FAIL } from '../../../yali-sdk/utils/fail';
import { updateVariableParams, showFail, showSuccess } from '../../config/common';
import bindOpenid from '../../../yali-sdk/business/getcash/bindOpenid';
import withdrawBindSend from '../../../yali-sdk/business/getcash/withdrawBindSend';
import { HOME_PATH } from '../pathUrl'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    second: 60,
    btn_Text: '发送验证码',
    btn: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ phone: options.phone });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },
  // 发送验证码
  sendCode() {
    const token = getIn(getApp(), ['globalData', 'token']);
    const shop_id = getIn(this.options, ['shop_id']);
    const phone = getIn(this.options, ['phone']);
    const params = { token, shop_id, phone };
    let second = this.data.second - 1;
    this.setData({
      second,
      btn_Text: `${second}s后重新获取`,
      btn: true,
    });
    const timer = setInterval(() => {
      second -= 1;
      if (second == 0) {
        clearInterval(timer);
        // 重新激活获取验证码按钮
        this.setData({
          second: 60,
          btn_Text: '重新发送验证码',
          btn: false,
        });
      } else {
        this.setData({
          second,
          btn_Text: `${second}s后重新获取`,
          btn: true,
        });
      }
    }, 1000);
    global.ActionDispatcher.execute(withdrawBindSend, params, (code, res) => {
      if(code == SUCCESS){
        showSuccess('发送成功');
      } else if(code == FAIL){
        showFail(res.msg)
      }
    })
  },
  // 填写验证码
  onChange(e) {
    const value = getIn(e.detail, ['value']);
    this.setData({ code: value });
  },
  submit() {
    const token = getIn(getApp(), ['globalData', 'token']);
    const shop_id = getIn(this.options, ['shop_id']);
    const params = { token, shop_id, code: this.data.code }; console.log(this.data.code)
    if (!this.data.code || this.data.code == '') {showFail('请填写验证码'); return;}
    global.ActionDispatcher.execute(bindOpenid, params, (code, res) => {
      if (code == SUCCESS) {
        showSuccess('绑定成功')
        global.navigateHelper.reLaunch({
          pathname: HOME_PATH
        });
      } else if(code == FAIL) {
        showFail(res.msg)
      }
    })
  }
})